import db from '../mongo'
import { parseTime } from '../middleware/comFunc/formatTime'
let sortSchema = db.Schema({
  id: Number,
  title: {
    type: String,
    unique: true,
  },
  value: {
    type: String,
    unique: true,
  },
  createAt: {                        // 创建日期
    type: String,
    default: ''
  },
  updateAt: {                        // 更新日期
    type: String,
    default: ''
  },
})

// 时间更新
sortSchema.pre('save', function (next) {
  if (this.isNew) {
    this.createAt = this.updateAt = parseTime(Date.now())
  } else {
    this.updateAt = Date.now()
  }
  next()
})
sortSchema.pre('update', function (next) {
   this.update({},{ $set: { updateAt: parseTime(Date.now()) } })
   next()
})

export default db.model('sort', sortSchema);
